home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PRINTER / PCLCM128.ARJ / GETOPT.C next >
C/C++ Source or Header  |  1991-03-13  |  1KB  |  57 lines

  1. #include <stdio.h>
  2.  
  3. #define index strchr
  4.  
  5. /*
  6.  * get option letter from argument vector
  7.  */
  8. int    opterr = 1,        /* useless, never set or used */
  9.     optind = 1,        /* index into parent argv vector */
  10.     optopt;            /* character checked for validity */
  11. char    *optarg;        /* argument associated with option */
  12.  
  13. #define BADCH    (int)'?'
  14. #define EMSG    ""
  15. #define tell(s)    fputs(*nargv,stderr);fputs(s,stderr); \
  16.         fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  17.  
  18. getopt(nargc,nargv,ostr)
  19. int    nargc;
  20. char    **nargv,
  21.     *ostr;
  22. {
  23.     static char    *place = EMSG;    /* option letter processing */
  24.     register char    *oli;        /* option letter list index */
  25.     char    *index();
  26.  
  27.     if(!*place) {            /* update scanning pointer */
  28.         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  29.         if (*place == '-') {    /* found "--" */
  30.             ++optind;
  31.             return(EOF);
  32.         }
  33.     }                /* option letter okay? */
  34.     if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
  35.         if(!*place) ++optind;
  36.         tell(": illegal option -- ");
  37.     }
  38.     if (*++oli != ':') {        /* don't need argument */
  39.         optarg = NULL;
  40.         if (!*place) ++optind;
  41.     }
  42.     else {                /* need an argument */
  43.         if (*place) optarg = place;    /* no white space */
  44.         else if (nargc <= ++optind) {    /* no arg */
  45.             place = EMSG;
  46.             tell(": option requires an argument -- ");
  47.         }
  48.          else optarg = nargv[optind];    /* white space */
  49.         place = EMSG;
  50.         ++optind;
  51.     }
  52.     return(optopt);            /* dump back option letter */
  53. }
  54.  
  55.  
  56.  
  57.